//
//  XRmenuBaseView.m
//  XRmenu
//
//  Created by Gao Yan on 11-11-23.
//  Copyright 2011年 foxhis. All rights reserved.
//

#import "XRmenuBaseView.h"

@implementation XRmenuBaseView
@synthesize tabDict, selectedIndex, viewDict;

#pragma mark -
#pragma mark 框架性初始化及释放方法
//=========================================================== 
// dealloc
//=========================================================== 
- (void)dealloc
{
	[tabDict release];
    tabDict = nil;
	[selectedIndex release];
	selectedIndex=nil;
	[viewDict release];
	viewDict = nil;
	
	[[NSNotificationCenter defaultCenter] removeObserver:self name:kMessage_System_Change_Language object:nil];
    
    [super dealloc];
}

//=========================================================== 
// initWithConfig:(NSDictionary*)cfg
//=========================================================== 
-(id) initWithConfig:(NSDictionary*)cfg
{
	if (cfg) {
		self.viewDict = cfg;
		CGRect f = CGRectFromString([cfg objectForKey:@"view rect"]);
		self = [self initWithFrame:f];
		if (self) {
			// Initialization code.
			if ([cfg objectForKey:@"background"]) {
				[self setBackgroundProperty:[cfg objectForKey:@"background"]];
			}
			if ([cfg objectForKey:@"imageview list"]) {
				[self setImageViewsProperty:[cfg objectForKey:@"imageview list"]];
			}
			if ([cfg objectForKey:@"videoview list"]) {
				[self setVideoViewsProperty:[cfg objectForKey:@"videoview list"]];
			}
			if ([cfg objectForKey:@"button list"]) {
				[self setButtonsProperty:[cfg objectForKey:@"button list"]];
			}
			if ([cfg objectForKey:@"label list"]) {
				[self setLabelsProperty:[cfg objectForKey:@"label list"]];
			}
		}
		return self;
		
	}else {
		NSAssert(FALSE, @"系统配置的样式数据格式有误");
		return nil;
	}	
}

//=========================================================== 
// initWithFrame:(CGRect)frame 指定大小及位置初始化view
//=========================================================== 
- (id)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code.
		[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeLanguage:) name:kMessage_System_Change_Language object:nil];
        
    }
    return self;
}

//=========================================================== 
// -(id) initWithFrame:(CGRect)frame Style:(NSString*)stylename
// 通用风格的view初始化方法
//=========================================================== 
-(id) initWithFrame:(CGRect)frame Style:(NSString*)stylename
{
	NSDictionary *tdict = [[APPSESSION.allconfig objectForKey:@"UIKit CSS"] objectForKey:@"UIView CSS"];
    
	NSDictionary *style;
	if ([tdict objectForKey:stylename]) {
		style = [tdict objectForKey:stylename];
	}else {
		style = [tdict objectForKey:@"default skin"];
	}
    
	BOOL atap = NO;
	if ([style objectForKey:@"allowTap"]) {
		atap = [(NSNumber*)[style objectForKey:@"allowTap"] boolValue];
	}
	
	BOOL aswipe = NO;
	if ([style objectForKey:@"allowSwipe"]) {
		aswipe = [(NSNumber*)[style objectForKey:@"allowSwipe"] boolValue];
	}
	self = [self initWithFrame:frame TapGesture:atap SwipeGesture:aswipe];
	if (self) {
		[self setBackgroundProperty:style];
	}
	
	return self;
}

//=========================================================== 
// -(id) initWithFrame:(CGRect)frame TapGesture:(BOOL)aTap SwipeGesture:(BOOL)aSwipe
// 通用的view初始化方法，带手势注册，sample code
//=========================================================== 
-(id) initWithFrame:(CGRect)frame TapGesture:(BOOL)aTap SwipeGesture:(BOOL)aSwipe
{
	self = [self initWithFrame:frame];
	if (self) {
		if (aTap) {
			UITapGestureRecognizer *recognizer;
			/*
			 Create a tap recognizer and add it to the view.
			 Keep a reference to the recognizer to test in gestureRecognizer:shouldReceiveTouch:.
			 */
			recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapFrom:)];
			recognizer.delegate = self;
			[self addGestureRecognizer:recognizer];
			[recognizer release];
		}
		if (aSwipe) {
			UISwipeGestureRecognizer *recognizer1;
            
			recognizer1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
			recognizer1.direction = UISwipeGestureRecognizerDirectionLeft||UISwipeGestureRecognizerDirectionRight;
			[self addGestureRecognizer:recognizer1];
			[recognizer1 release];
		}
	}
	return self;
}

#pragma mark 指定UI 样式的初始化view方法
-(id) initWithStyle:(NSString*)stylename ViewName:(NSString*)vname
{
	NSDictionary *tdict = [[[APPSESSION.allconfig objectForKey:@"UI Skin"] objectForKey:stylename] objectForKey:vname];
	if (tdict) {
		return [self initWithConfig:tdict];
	}else {
		NSAssert(FALSE, @"系统配置的样式数据格式有误");
		return nil;
	}
}

-(id) initWithStyle:(NSString*)stylename DishSubViewName:(NSString*)vname
{
	NSDictionary *tdict = [APPSESSION.allconfig findAtPath:[NSString stringWithFormat:@"UI Skin/%@/%@", stylename, vname]];
	if (tdict) {
		return [self initWithConfig:tdict];
	}else {
		NSAssert(FALSE, @"系统配置的样式数据格式有误");
		return nil;
	}
}
#pragma mark -
#pragma mark 依据一定结构的plist，通用增加背景设置及按钮绘制
-(void) loadAllObjectByConfig:(NSDictionary*)cfg
{
    if (self) {
        // Initialization code.
        if ([cfg objectForKey:@"background"]) {
            [self setBackgroundProperty:[cfg objectForKey:@"background"]];
        }
        if ([cfg objectForKey:@"imageview list"]) {
            [self setImageViewsProperty:[cfg objectForKey:@"imageview list"]];
        }
        if ([cfg objectForKey:@"videoview list"]) {
            [self setVideoViewsProperty:[cfg objectForKey:@"videoview list"]];
        }
        if ([cfg objectForKey:@"button list"]) {
            [self setButtonsProperty:[cfg objectForKey:@"button list"]];
        }
        if ([cfg objectForKey:@"label list"]) {
            [self setLabelsProperty:[cfg objectForKey:@"label list"]];
        }
    }
}

-(void) setBackgroundProperty:(NSDictionary*)bdict
{
	BOOL isImg = NO;
	isImg = [bdict getBoolValAtPath:@"background is image"];
	NSString *bgname;
	if (isImg) {
		NSDictionary *tr = [APPSESSION.translate objectForKey:[bdict getStringValueAtPath:@"background image"]];
		if (tr) {
			bgname = [XRmenuPublic getGeneralSkinImg:[tr objectForKey:APPSESSION.language]];
		}else {
			bgname = [XRmenuPublic getGeneralSkinImg:[bdict getStringValueAtPath:@"background image"]];
		}
		if ([bgname isEqualToString:@"No Image"]) {
			// 无图
			[self setBackgroundColor:[UIColor blueColor]];
		}else {
			// 有图
			UIImageView *bgv = [[UIImageView alloc] initWithFrame:self.bounds];
			UIImage *bgimg = [UIImage imageWithContentsOfFile:bgname];
			[bgv setImage:bgimg];
			[bgv setTag:kTagOfBgImage];
			[self addSubview:bgv];
			//[bgimg release];
			[bgv release];
		}
        
	}else {
		UIColor *bgc = [XRmenuPublic generalColor:[bdict objectForKey:@"background color"]];
		[self setBackgroundColor:bgc];
	}	
}

-(void) setImageViewsProperty:(NSArray *)imvs
{
	UIImageView *imgV;
	CGRect content = self.bounds;
	for (NSDictionary *item in imvs) {
		imgV = nil;
		CGRect imgVframe = CGRectOffset(CGRectFromString([item objectForKey:@"imageview rect"]), content.origin.x, content.origin.y);
		imgV=[XRmenuPublic generalImageView:imgVframe];
		[imgV setTag:(kTagOfStartImgV + dictInt(item, @"imageview id"))]; 
		
		if ([item objectForKey:@"imageview url"]) {
			NSDictionary *tr = [APPSESSION.translate objectForKey:[item objectForKey:@"imageview url"]];
			NSString *imgname;
			if (tr) {
				imgname = [XRmenuPublic getGeneralSkinImg:[tr objectForKey:APPSESSION.language]];
			}else {
				if ([item getBoolValAtPath:@"without skin"]) {
					imgname = [XRmenuPublic getGeneralImg:[item objectForKey:@"imageview url"]];
				}else {
					imgname = [XRmenuPublic getGeneralSkinImg:[item objectForKey:@"imageview url"]];
				}
			}	
			[imgV setImage:[UIImage imageWithContentsOfFile:imgname]];			
		}
		[self addSubview:imgV];
	}
}

-(void) setVideoViewsProperty:(NSArray *)vds
{
	VideoPlayer *player;
	CGRect content = self.bounds;
	for (NSDictionary *item in vds) {
		player = nil;
		CGRect vdVframe = CGRectOffset(CGRectFromString([item objectForKey:@"videoview rect"]), content.origin.x, content.origin.y);
		player=[XRmenuPublic generalVideoView:vdVframe];
		
		[player setTag:(kTagOfStartVideo + dictInt(item, @"videoview id"))];
		
		[self addSubview:player];
	}
}

-(void) setLabelsProperty:(NSArray*)lbs
{
	UILabel *lb;
	CGRect content = self.bounds;
	for (NSDictionary *item in lbs) {
		lb = nil;
		CGRect lbframe = CGRectOffset(CGRectFromString([item objectForKey:@"label rect"]), content.origin.x, content.origin.y);
        
		lb=[XRmenuPublic generalLabel:[item objectForKey:@"label style"] LabelText:[item objectForKey:@"text"] LabelFrame:lbframe];
		[lb setTag:(kTagOfStartLabel + dictInt(item, @"label id"))];
		        
		[self addSubview:lb];
	}
}

-(void) setButtonsProperty:(NSArray*)bts
{
	UIButton *bt;
	CGRect content = self.bounds;
	NSDictionary *ldict;
	for (NSDictionary *item in bts) {
		bt = nil;
		CGRect btframe = CGRectOffset(CGRectFromString([item objectForKey:@"button rect"]), content.origin.x, content.origin.y);
		ldict = nil;
		if ([item objectForKey:@"button lable"]) {
			ldict = [item objectForKey:@"button lable"];
		}
        
		bt = [XRmenuPublic generalButton:[item objectForKey:@"button style"] ButtonLable:[ldict objectForKey:@"state lable"] ButtonFrame:btframe];
		[bt setTag:(kTagOfStartBT + dictInt(item, @"button id"))];
		[bt addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];
		
		if ([ldict objectForKey:@"font style"]) {
			[[bt titleLabel] setFont:[XRmenuPublic generalFont:[ldict objectForKey:@"font style"]]];
		}else {
			[[bt titleLabel] setFont:[XRmenuPublic generalFont:@"default font"]];
		}
		[self addSubview:bt];
	}
}

-(void) setTextViewProperty:(NSDictionary*)tdict WithContent:(NSString *)content{
	UITextView *tmpV =  [[[UITextView alloc] initWithFrame:CGRectFromString([tdict getStringValueAtPath:@"text rect"])] autorelease];
	tmpV.textColor = [XRmenuPublic generalColor:[tdict objectForKey:@"text color"]];
    tmpV.font=[XRmenuPublic generalFont:[tdict objectForKey:@"font style"]];
	tmpV.delegate = self;
	tmpV.backgroundColor = [XRmenuPublic generalColor:[tdict objectForKey:@"background color"]];
	if (content) {
		tmpV.text = content;
	}else {
		tmpV.text = @"";
	}
	tmpV.returnKeyType = UIReturnKeyDefault;
	tmpV.keyboardType = UIKeyboardTypeDefault;
	tmpV.scrollEnabled = YES;
	tmpV.autoresizingMask = UIViewAutoresizingFlexibleHeight;
	[tmpV setTag:kTagOfStartText];
	
	[self addSubview:tmpV];  
}

-(void) setTableViewProperty:(NSDictionary*)tdict Frame:(CGRect)fr
{
	self.tabDict = tdict;
	//kTagOfStartTableView
	UITableView *tview = [[UITableView alloc] initWithFrame:fr style:UITableViewStylePlain];
	// 依据字典设置背景
	if ([tdict objectForKey:@"background"]) {
		NSDictionary *bdict = [tdict objectForKey:@"background"];
		BOOL isimg = NO;
		if ([bdict objectForKey:@"background is image"]) {
			isimg = [(NSNumber*)[bdict objectForKey:@"background is image"] boolValue];
		}
		if (isimg) {
			UIImageView *bgv = [[UIImageView alloc] initWithFrame:fr];
			UIImage *bgimg = [[UIImage alloc] initWithContentsOfFile:[XRmenuPublic getGeneralSkinImg:[bdict objectForKey:@"background image"]]];
			[bgv setImage:bgimg];
			[tview setBackgroundView:bgv];
			[bgimg release];
			[bgv release];
			[tview setBackgroundColor:[UIColor brownColor]];
		}else {
			[tview setBackgroundColor:[XRmenuPublic generalColor:[bdict objectForKey:@"background color"]]];
		}		
	}else {
		[tview setBackgroundColor:[UIColor clearColor]];
	}
	// 依据字典设置row
	if ([tdict objectForKey:@"row property"]){
		NSDictionary *rdict = [tdict objectForKey:@"row property"];
		BOOL showrow = NO;
		if ([rdict objectForKey:@"row show"]) {
			showrow = [(NSNumber*)[rdict objectForKey:@"row show"] boolValue];
		}
		if (showrow) {
			[tview setRowHeight:[[rdict objectForKey:@"row height"] floatValue]];			
		}else {
			[tview setRowHeight:0.0f];
		}
	}else {
		[tview setRowHeight:0.0f];
	}	
	// 依据字典设置section
	if ([tdict objectForKey:@"section property"]){
		NSDictionary *sdict = [tdict objectForKey:@"section property"];
		BOOL showsection = NO;
		if ([sdict objectForKey:@"section show"]) {
			showsection = [(NSNumber*)[sdict objectForKey:@"section show"] boolValue];
		}
		if (showsection) {
			[tview setSectionHeaderHeight:[[sdict objectForKey:@"section height"] floatValue]];			
		}else {
			[tview setSectionHeaderHeight:0.0f];
		}
	}else {
		[tview setSectionHeaderHeight:0.0f];
	}
	// 依据字典设置foot
	if ([tdict objectForKey:@"foot property"]){
		NSDictionary *fdict = [tdict objectForKey:@"foot property"];
		BOOL showsection = NO;
		if ([fdict objectForKey:@"foot show"]) {
			showsection = [(NSNumber*)[fdict objectForKey:@"foot show"] boolValue];
		}
		if (showsection) {
			[tview setSectionHeaderHeight:[[fdict objectForKey:@"foot height"] floatValue]];			
		}else {
			[tview setSectionHeaderHeight:0.0f];
		}
	}else {
		[tview setSectionFooterHeight:0.0f];
	}
	
	// 依据字典设置table head
	if ([tdict objectForKey:@"tabhead property"]){
		NSDictionary *thdict = [tdict objectForKey:@"tabhead property"];
		BOOL showtabhead = NO;
		if ([thdict objectForKey:@"tabhead show"]) {
			showtabhead = [(NSNumber*)[thdict objectForKey:@"tabhead show"] boolValue];
		}
		if (showtabhead) {
			XRmenuBaseView *tmp = [[XRmenuBaseView alloc] initWithConfig:thdict];
			[tview setTableHeaderView:tmp];
			[tmp release];
		}
	}
    
	// 依据字典设置table foot
	if ([tdict objectForKey:@"tabfoot property"]){
		NSDictionary *tfdict = [tdict objectForKey:@"tabfoot property"];
		BOOL showtabfoot = NO;
		if ([tfdict objectForKey:@"tabfoot show"]) {
			showtabfoot = [(NSNumber*)[tfdict objectForKey:@"tabfoot show"] boolValue];
		}
		if (showtabfoot) {
			XRmenuBaseView *tmp = [[XRmenuBaseView alloc] initWithConfig:tfdict];
			[tview setTableFooterView:tmp];
			[tmp release];
		}
	}
	
	[tview setDelegate:self];
	[tview setDataSource:self];
	// This should be set to work with the image height
	[tview setAllowsSelection:YES];
	[tview setSeparatorStyle:UITableViewCellSeparatorStyleNone];
	[tview setTag:kTagOfStartTableView];
	[self addSubview:tview];
	[tview release];
}

#pragma mark 依据参数调整部分值
-(CGRect) adjustRectByBoxStyle:(NSString*)stylename Frame:(CGRect)fr
{
	NSDictionary *boxdict = [APPSESSION.allconfig findAtPath:[NSString stringWithFormat:@"UIKit CSS/Box CSS/%@", stylename]];
	CGRect ret = CGRectMake(0, 0, fr.size.width, fr.size.height);
	CGPoint p;
	if ([boxdict objectForKey:@"inset offset"]) {
		p = CGPointFromString([boxdict objectForKey:@"inset offset"]);
	}else {
		p = CGPointMake(10, 10);
	}
	return CGRectInset(ret, p.x, p.y);
}

#pragma mark 默认按钮处理时间，一般需重载
-(void) buttonClicked:(id)sender
{
	DebugLog(@"请重载本方法处理按钮事件%i", [sender tag]);
}

#pragma mark 语言切换
-(void)changeLanguage:(NSNotification*)notification
{
	NSDictionary *info = [notification userInfo];
	DebugLog(@"语言切换到 %@, 请重载", info);
}

-(void) translateButtons:(NSArray*)bts
{
	NSDictionary *ldict;
	for (NSInteger ii = 0; ii < [bts count]; ii++) {
		NSDictionary *item = [bts objectAtIndex:ii];
		if ([item objectForKey:@"button lable"]) {
			ldict = nil;
			ldict = [item findAtPath:@"button lable/state lable"];
			if (ldict) {
				if ([self viewWithTag:(kTagOfStartBT+ii)]) {
					UIButton *bt = (UIButton*)[self viewWithTag:(kTagOfStartBT+ii)];
					if (bt) {
						if ([ldict getBoolValAtPath:@"lable is image"]) {
							//todo:按语言换图片
							[XRmenuPublic adjustButtonsImage:bt Parms:ldict];
						}else {
							if ([ldict objectForKey:@"Normal"]) {
								[bt setTitle:loc([ldict objectForKey:@"Normal"]) forState:UIControlStateNormal];
							}
							if ([ldict objectForKey:@"Highlighted"]) {
								[bt setTitle:loc([ldict objectForKey:@"Highlighted"]) forState:UIControlStateHighlighted];
							}
							if ([ldict objectForKey:@"Disabled"]) {
								[bt setTitle:loc([ldict objectForKey:@"Disabled"]) forState:UIControlStateDisabled];
							}
							if ([ldict objectForKey:@"Selected"]) {
								[bt setTitle:loc([ldict objectForKey:@"Selected"]) forState:UIControlStateSelected];
							}					
						}
					}
				}
			}
		}
	}	
}

#pragma mark -
#pragma mark Tableview delegate and datasource
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	NSAssert(NO, @"必须重载本方法用于显示不同数量的cell");
	return 0;
}

-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
	NSAssert(NO, @"必须重载本方法用于显示不同数量的sections");
	return 1;
}

-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	NSAssert(NO, @"必须重载本方法用于显示不同的cell");
	return nil;
}

- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	selectedIndex = indexPath;
	NSAssert(NO, @"必须重载本方法用于处理cell的点击事件");
}

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // The table view should not be re-orderable.
    return NO;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
	if(selectedIndex != nil && [selectedIndex compare:indexPath] == NSOrderedSame) 
	{
		// 当前选择的行
		if ([self.tabDict objectForKey:@"row property"]){
			NSDictionary *rdict = [self.tabDict objectForKey:@"row property"];
			BOOL showrow = NO;
			if ([rdict objectForKey:@"row show"]) {
				showrow = [(NSNumber*)[rdict objectForKey:@"row show"] boolValue];
			}
			if (showrow) {
				return [[rdict objectForKey:@"expand row height"] floatValue];
			}else {
				return 0.0f;
			}
		}else {
			return 0.0f;
		}	
	}else {
		// 非当前选择的行
		if ([self.tabDict objectForKey:@"row property"]){
			NSDictionary *rdict = [self.tabDict objectForKey:@"row property"];
			BOOL showrow = NO;
			if ([rdict objectForKey:@"row show"]) {
				showrow = [(NSNumber*)[rdict objectForKey:@"row show"] boolValue];
			}
			if (showrow) {
				return [[rdict objectForKey:@"row height"] floatValue];
			}else {
				return 0.0f;
			}
		}else {
			return 0.0f;
		}	
	}
}

-(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
	// 依据字典设置head
	if ([self.tabDict objectForKey:@"head property"]){
		NSDictionary *hdict = [self.tabDict objectForKey:@"head property"];
		BOOL showhead = NO;
		if ([hdict objectForKey:@"head show"]) {
			showhead = [(NSNumber*)[hdict objectForKey:@"head show"] boolValue];
		}
		if (showhead) {
			return [[hdict objectForKey:@"head height"] floatValue];
		}else {
			return 0.0f;
		}
	}else {
		return 0.0f;
	}
}

-(CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
	// 依据字典设置foot
	if ([self.tabDict objectForKey:@"foot property"]){
		NSDictionary *fdict = [self.tabDict objectForKey:@"foot property"];
		BOOL showfoot = NO;
		if ([fdict objectForKey:@"foot show"]) {
			showfoot = [(NSNumber*)[fdict objectForKey:@"foot show"] boolValue];
		}
		if (showfoot) {
			return [[fdict objectForKey:@"foot height"] floatValue];
		}else {
			return 0.0f;
		}
	}else {
		return 0.0f;
	}
}
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
	// 依据字典设置head
	if ([self.tabDict objectForKey:@"head property"]){
		NSDictionary *hdict = [self.tabDict objectForKey:@"head property"];
		BOOL showhead = NO;
		if ([hdict objectForKey:@"head show"]) {
			showhead = [(NSNumber*)[hdict objectForKey:@"head show"] boolValue];
		}
		if (showhead) {
			XRmenuBaseView *tmp = [[[XRmenuBaseView alloc] initWithConfig:hdict] autorelease];
			return tmp;
		}else {
			return nil;
		}
	}else {
		return nil;
	}
}
-(UIView*) tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
	// 依据字典设置foot
	if ([self.tabDict objectForKey:@"foot property"]){
		NSDictionary *fdict = [self.tabDict objectForKey:@"foot property"];
		BOOL showfoot = NO;
		if ([fdict objectForKey:@"foot show"]) {
			showfoot = [(NSNumber*)[fdict objectForKey:@"foot show"] boolValue];
		}
		if (showfoot) {
			XRmenuBaseView *tmp = [[[XRmenuBaseView alloc] initWithConfig:fdict] autorelease];
			return tmp;
		}else {
			return nil;
		}
	}else {
		return nil;
	}
}
#pragma mark -
#pragma mark 独立增加各类手势
- (void) add1fingerTap
{
	UITapGestureRecognizer *recognizer;
	/*
	 Create a tap recognizer and add it to the view.
	 Keep a reference to the recognizer to test in gestureRecognizer:shouldReceiveTouch:.
	 */
	recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapFrom:)];
	recognizer.delegate = self;
	[self addGestureRecognizer:recognizer];
	[recognizer release];	
}

- (void) add1fingerTopSwipe
{
	UISwipeGestureRecognizer *recognizer1;
	
	recognizer1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeTop:)];
	recognizer1.direction = UISwipeGestureRecognizerDirectionUp;
	[self addGestureRecognizer:recognizer1];
	[recognizer1 release];
	
}

- (void) add1fingerDownSwipe
{
	UISwipeGestureRecognizer *recognizer1;
	
	recognizer1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeDown:)];
	recognizer1.direction = UISwipeGestureRecognizerDirectionDown;
	[self addGestureRecognizer:recognizer1];
	[recognizer1 release];
	
}

- (void) add1fingerLeftSwipe
{
	UISwipeGestureRecognizer *recognizer1;
	
	recognizer1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeLeft:)];
	recognizer1.direction = UISwipeGestureRecognizerDirectionLeft;
	[self addGestureRecognizer:recognizer1];
	[recognizer1 release];
	
}

- (void) add1fingerRightSwipe
{
	UISwipeGestureRecognizer *recognizer1;
	
	recognizer1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeRight:)];
	recognizer1.direction = UISwipeGestureRecognizerDirectionRight;
	[self addGestureRecognizer:recognizer1];
	[recognizer1 release];
}

- (void) addPinch
{
	UIPinchGestureRecognizer *recognizer1;
	
	recognizer1 = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinchForm:)];
	[self addGestureRecognizer:recognizer1];
	[recognizer1 release];	
}
#pragma mark -
#pragma mark 所有Gesture处理
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
	if ([touch.view isKindOfClass:[UIButton class]] || [[[touch.view class] description] isEqualToString:@"UITableViewCellContentView"] 
		|| ([touch.view class]  == NSClassFromString(@"DishAlbumView"))
		) 
	{
		DebugLog(@"按在按钮之类的对象上了");
		return NO;
	}
	DebugLog(@"如果需要屏蔽某些手势，请重载本方法 view class: %@ ? %@", [touch.view description], [[touch.view class] description]);
	
	return YES;
}

- (void)handleTapFrom:(UITapGestureRecognizer *)recognizer {
	CGPoint location = [recognizer locationInView:self];
	DebugLog(@"你用%i个手指点击了%@", [recognizer numberOfTouches], NSStringFromCGPoint(location));
}

- (void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {
	CGPoint location = [recognizer locationInView:self];
	DebugLog(@"你用%i个手指划过了%@", [recognizer numberOfTouches], NSStringFromCGPoint(location));
}

- (void)handleSwipeTop:(UISwipeGestureRecognizer *)recognizer {
	CGPoint location = [recognizer locationInView:self];
	DebugLog(@"你用%i个手指向上划过了%@", [recognizer numberOfTouches], NSStringFromCGPoint(location));
}

- (void)handleSwipeDown:(UISwipeGestureRecognizer *)recognizer {
	CGPoint location = [recognizer locationInView:self];
	DebugLog(@"你用%i个手指向下划过了%@", [recognizer numberOfTouches], NSStringFromCGPoint(location));
}

- (void)handleSwipeLeft:(UISwipeGestureRecognizer *)recognizer {
	CGPoint location = [recognizer locationInView:self];
	DebugLog(@"你用%i个手指划过了%@", [recognizer numberOfTouches], NSStringFromCGPoint(location));
}

- (void)handleSwipeRight:(UISwipeGestureRecognizer *)recognizer {
	CGPoint location = [recognizer locationInView:self];
	DebugLog(@"你用%i个手指划过了%@", [recognizer numberOfTouches], NSStringFromCGPoint(location));
}

- (void)handlePinchForm:(UIPinchGestureRecognizer *)recognizer
{
	DebugLog(@"请重载，scale: %.1f, velocity: %.1f", recognizer.scale, recognizer.velocity);
}

#pragma mark -
#pragma mark 字典中取数据
-(id)findNode:(NSDictionary*) aDic atPath:(NSString*)aPath
{
	NSArray *chunks = [aPath componentsSeparatedByString: @"/"];
	NSDictionary * res = [aDic copy];
	NSInteger iIndex =0;
	while (iIndex < [chunks count]) {
		res =[res objectForKey:[chunks objectAtIndex:iIndex]];
		iIndex++;
	}  
	return [res autorelease];
}

-(id)findNode:(NSDictionary*) aDic withKey:(NSString*)aKey
{
	id res = nil;
	NSArray* keys = [aDic allKeys]; 
	for (NSString* key in keys){
		if ([key isEqualToString:aKey]) {
			res = [aDic objectForKey:aKey];
			break;
		}
	}
	if (res) {
		return res;
	}
	else {
		for (NSString* key in keys){ 
			if (![NSStringFromClass([[aDic objectForKey:key] class]) isEqualToString:@"__NSCFDictionary"] ) {
				continue;
			} 
			id res2 =  [self findNode:[aDic objectForKey:key] withKey:aKey];
			
			if (res2) {
				return res2;
			}  
		}
	}
	return res;
}
//
-(void)attach1fingerTap:(UIView*)aView sel:(SEL)aSel
{
	UITapGestureRecognizer *recognizer; 
	recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:aSel];
	recognizer.delegate = self;
	[aView addGestureRecognizer:recognizer];
	[recognizer release];	 
}
@end